/**
 * 作者: wgx
 * 日期: 2025-02-26 14:31:53
 * 备注: 用户怪兽洞窟模块数据
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserCaveLocation;
import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.server.game.commondata.cave.CaveMain;
import com.motu.monstercity.server.game.logic.BuildManager;
import com.motu.monstercity.server.game.logic.CaveManager;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import io.vertx.core.json.JsonObject;

import java.util.List;

public class UserCaveLocation extends UserCaveLocationModel {
    public UserCaveLocation() {
        super();
    }

    public JsonObject insert() {
        putAreaType(1);// 初始时默认从第1区域，最底层开始
        putFloorId(0);// 从0 开始
        return super.insert();
    }

    public PBUserCaveLocation.Builder takeInitBuilder() {
        PBUserCaveLocation.Builder sb = super.takeInitBuilder();
        CaveMain caveMain = CaveManager.getCaveMain(getAreaType(), getFloorId());
        if (caveMain != null) {
            sb.setCaveMainId(caveMain.getId());
        }
        return sb;
    }

    public PBUserCaveLocation.Builder takeBuilder() {
        PBUserCaveLocation.Builder sb = super.takeBuilder();
        CaveMain caveMain = CaveManager.getCaveMain(getAreaType(), getFloorId());
        if (caveMain != null) {
            sb.setCaveMainId(caveMain.getId());
        }
        return sb;
    }

    // 怪兽洞窟对商业建筑的收益加成
    public int takeEarnAdd() {
        CaveMain caveMain = CaveManager.getCaveMain(getAreaType(), getFloorId());
        if (caveMain == null) {
            return 0;
        }
        return caveMain.getEarnSpeedAddition();
    }


    // 地下洞窟更新时，批量更新商业建筑的收益
    public void updateEarnAdd(PBUserData.Builder pbUserData, UserInfo userInfo, UserPower userPower) {
        update();
        int addValue = takeEarnAdd();
        if (addValue != userPower.getEarnAddCavePer()) {
            userPower.putEarnAddCavePer(addValue);
            List<UserBuild> buildList = userPower.takeUserBuildList(0);// 会更新所有的商业建筑收益
            BuildManager.batchUpdateBuildEarn(pbUserData, userInfo, userPower, buildList);
        }
    }
}
